package com.itfreer.utils.http;

import com.itfreer.utils.common.JSONUtils;
import com.itfreer.utils.config.ConfigManager;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 定义远程访问辅助类
 */
public class HttpUtils {

    /**
     * Post请求
     * @param url
     * @param params
     * @param entityClass
     * @param <T>
     * @return
     */
    public static <T> T post(String url, Object params, Class<T> entityClass) throws Exception {
        URL downUrl = new URL(url);
        HttpURLConnection con = (HttpURLConnection) downUrl.openConnection();
        // 发送POST请求必须设置如下两行
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
        con.setRequestProperty("referer", "android");
        con.setRequestProperty("user-agent", "webkit");
        if(ConfigManager.getConfig("jwt")!=null && !ConfigManager.getConfig("jwt").toString().trim().equals("")){
            con.setRequestProperty("jwt", ConfigManager.getConfig("jwt").toString());
        }
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        if(params!=null){
            ds.write(JSONUtils.toJSONString(params).getBytes());
            ds.flush();
        }

        // 定义BufferedReader输入流来读取URL的响应
        InputStream inputStream = con.getInputStream();

        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        String s = result.toString(StandardCharsets.UTF_8.name());

        T t = (T)JSONUtils.toBean(s, entityClass);
        ds.close();
        return t;
    }

    /**
     * Post请求
     * @param url
     * @param params
     * @return
     */
    public static void post(String url, Object params) throws Exception {
        URL downUrl = new URL(url);
        HttpURLConnection con = (HttpURLConnection) downUrl.openConnection();
        // 发送POST请求必须设置如下两行
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
        con.setRequestProperty("referer", "android");
        con.setRequestProperty("user-agent", "webkit");
        if(ConfigManager.getConfig("jwt")!=null && !ConfigManager.getConfig("jwt").toString().trim().equals("")){
            con.setRequestProperty("jwt", ConfigManager.getConfig("jwt").toString());
        }
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        if(params!=null){
            ds.write(JSONUtils.toJSONString(params).getBytes());
            ds.flush();
        }

        // 定义BufferedReader输入流来读取URL的响应
        con.getInputStream();
        ds.close();
    }

    /**
     * Post请求
     * @param url
     * @param params
     * @return
     */
    public static void delete(String url, Object params) throws Exception {
        URL downUrl = new URL(url);
        HttpURLConnection con = (HttpURLConnection) downUrl.openConnection();
        // 发送POST请求必须设置如下两行
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestMethod("DELETE");
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
        con.setRequestProperty("referer", "android");
        con.setRequestProperty("user-agent", "webkit");
        if(ConfigManager.getConfig("jwt")!=null && !ConfigManager.getConfig("jwt").toString().trim().equals("")){
            con.setRequestProperty("jwt", ConfigManager.getConfig("jwt").toString());
        }
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        if(params!=null){
            ds.write(JSONUtils.toJSONString(params).getBytes());
            ds.flush();
        }

        // 定义BufferedReader输入流来读取URL的响应
        con.getInputStream();
        ds.close();
    }

    /**
     * Post请求
     * @param url
     * @param params
     * @param entityClass
     * @param <T>
     * @return
     */
    public static <T> List<T> postList(String url, Object params, Class<T> entityClass) throws Exception {
        URL downUrl = new URL(url);
        HttpURLConnection con = (HttpURLConnection) downUrl.openConnection();
        // 发送POST请求必须设置如下两行
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
        con.setRequestProperty("referer", "android");
        con.setRequestProperty("user-agent", "webkit");
        if(ConfigManager.getConfig("jwt")!=null && !ConfigManager.getConfig("jwt").toString().trim().equals("")){
            con.setRequestProperty("jwt", ConfigManager.getConfig("jwt").toString());
        }
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        if(params!=null){
            ds.write(JSONUtils.toJSONString(params).getBytes());
            ds.flush();
        }

        // 定义BufferedReader输入流来读取URL的响应
        InputStream inputStream = con.getInputStream();

        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        String s = result.toString(StandardCharsets.UTF_8.name());

        List<T> lt = (List<T>)JSONUtils.toListBean(s, entityClass);
        ds.close();
        return lt;
    }
}
